const path = require('path');

function resolve(...rest) {
  return path.resolve(__dirname, '..', ...rest);
}

/**
 * @param {number} num
 * @param {number} count
 */
function formatNumber(num, count) {
  const m = 10 ** count;
  if (num >= m) {
    return num.toString();
  }
  return (num + m).toString().slice(1);
}

/**
 * @param {Date} date 日期
 */
function formatDate(date = new Date()) {
  const year = formatNumber(date.getFullYear(), 2);
  const month = formatNumber(date.getMonth() + 1, 2);
  const day = formatNumber(date.getDate(), 2);
  const hours = formatNumber(date.getHours(), 2);
  const minutes = formatNumber(date.getMinutes(), 2);
  const seconds = formatNumber(date.getSeconds(), 2);

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

/**
 * @param {string} dir
 */
function getAlias(dir) {
  return {
    '@': path.resolve(dir, 'src'),
  };
}

/**
 * @param {string} id
 */
function relativeId(id) {
  const cwd = process.cwd();
  return id.replace(cwd, '');
}

module.exports = {
  resolve,
  formatNumber,
  formatDate,
  getAlias,
  relativeId,
};
